{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "53af08cf-0149-4369-93d6-fba2203ec6cc",
   "metadata": {},
   "source": [
    "# Upload to Cloud Object Storage COS HTTP adapter\n",
    "\n",
    "Create a HTTP service to upload a file to any S3 compliant Cloud Object Storage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9ee25986-e86d-4995-9a1d-0e2ea95e35ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "#os.environ['create_image']='True'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "38ee13e4-dd59-4b52-982a-ea5ba3f6594e",
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sending build context to Docker daemon  39.94kB\n",
      "Step 1/5 : FROM registry.access.redhat.com/ubi8/python-39\n",
      " ---> a531ae76755e\n",
      "Step 2/5 : RUN pip install ipython nbformat aiobotocore botocore s3fs flask\n",
      " ---> Using cache\n",
      " ---> f349e56a05a3\n",
      "Step 3/5 : ADD upload-to-cos.ipynb /\n",
      " ---> 8a007fd5de44\n",
      "Step 4/5 : ADD upload-to-cos-http-adapter.ipynb /\n",
      " ---> cf63d3c9918f\n",
      "Step 5/5 : ENTRYPOINT [\"ipython\",\"/upload-to-cos-http-adapter.ipynb\"]\n",
      " ---> Running in 276421fbdf6a\n",
      "Removing intermediate container 276421fbdf6a\n",
      " ---> 30367437d548\n",
      "Successfully built 30367437d548\n",
      "Successfully tagged upload-to-cos-http-adapter:latest\n",
      "Using default tag: latest\n",
      "The push refers to repository [docker.io/romeokienzler/upload-to-cos-http-adapter]\n",
      "\n",
      "\u001b[1B9fa60caf: Preparing \n",
      "\u001b[1Ba6c2215a: Preparing \n",
      "\u001b[1Bb4330ba2: Preparing \n",
      "\u001b[1B2e0f4ef5: Preparing \n",
      "\u001b[1B276847a2: Preparing \n",
      "\u001b[1B534f4e1b: Preparing \n",
      "\u001b[1Bc926eef9: Preparing \n",
      "\u001b[8B9fa60caf: Pushed lready exists 4kB\u001b[5A\u001b[2K\u001b[4A\u001b[2K\u001b[3A\u001b[2K\u001b[1A\u001b[2K\u001b[7A\u001b[2K\u001b[8A\u001b[2Klatest: digest: sha256:4ece79573cbbb8988f04fd5a10ada81b5f1db70590bced8eb29f43616f76cad4 size: 2002\n"
     ]
    }
   ],
   "source": [
    "if bool(os.environ.get('create_image',False)):\n",
    "    docker_file=\"\"\"\n",
    "    FROM registry.access.redhat.com/ubi8/python-39\n",
    "    RUN pip install ipython nbformat aiobotocore botocore s3fs flask\n",
    "    ADD upload-to-cos.ipynb /\n",
    "    ADD upload-to-cos-http-adapter.ipynb /\n",
    "    ENTRYPOINT [\"ipython\",\"/upload-to-cos-http-adapter.ipynb\"]\n",
    "    \"\"\"\n",
    "    with open(\"Dockerfile\", \"w\") as text_file:\n",
    "        text_file.write(docker_file)\n",
    "\n",
    "    !docker build -t upload-to-cos-http-adapter .\n",
    "    !docker tag upload-to-cos-http-adapter romeokienzler/upload-to-cos-http-adapter\n",
    "    !docker push romeokienzler/upload-to-cos-http-adapter\n",
    "elif bool(os.environ.get('install_dependencies',False)):\n",
    "    !pip install aiobotocore botocore s3fs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff2f0a49-bdff-4271-90d5-1735a0c93489",
   "metadata": {},
   "outputs": [],
   "source": [
    "from flask import request\n",
    "from flask import Flask\n",
    "import time\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60436d8a-f461-4723-abeb-cc22b555c782",
   "metadata": {},
   "outputs": [],
   "source": [
    "app = Flask(__name__)\n",
    "\n",
    "@app.route('/', methods=['POST'])\n",
    "def index():\n",
    "    message = json.dumps(request.get_json())\n",
    "    print(message)\n",
    "    now = time.time()\n",
    "    print(now)\n",
    "    with open('/tmp/source.json','w') as file:\n",
    "        file.write(message)\n",
    "    !ipython /upload-to-cos.ipynb source_file=\"source.json\" destination_file=$now\".json\"\n",
    "    return ''\n",
    "\n",
    "app.run(host='0.0.0.0', port=8080)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
